package cn.tedu.kcircle.passport.adapter.controller;


import cn.tedu.kcircle.common.po.CurrentPrincipal;
import cn.tedu.kcircle.common.restful.JsonResult;
import cn.tedu.kcircle.passport.adapter.converter.PassportAdapterConverter;
import cn.tedu.kcircle.passport.adapter.vo.UserLoginResultVO;
import cn.tedu.kcircle.passport.protocol.param.UserLoginInfoParam;
import cn.tedu.kcircle.passport.protocol.param.UserRegisterParam;
import cn.tedu.kcircle.passport.service.bo.UserLoginResultBO;
import cn.tedu.kcircle.passport.service.PassportService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
@Tag(name="认证中心")
@Slf4j
@AllArgsConstructor
//@Validated
public class UserController {
    private PassportService passportService;
    private PassportAdapterConverter converter;
    @PostMapping("login")
    @Operation(summary = "单点登录")
    public JsonResult<UserLoginResultVO> login(@ParameterObject UserLoginInfoParam userLoginInfoParam, HttpServletRequest request){
        UserLoginResultBO userLoginResultBO = passportService.login(userLoginInfoParam,request);
        return JsonResult.ok(converter.BO2VO(userLoginResultBO));
    }
    @PostMapping("logOut")
    @Operation(summary = "退出登录")
    public JsonResult<Void> logOut(@AuthenticationPrincipal CurrentPrincipal principal){
        passportService.logOut(principal);
        return JsonResult.ok();
    }
    @PostMapping("register")
    @Operation(summary = "用户注册")
    public JsonResult<Void> register(@ParameterObject UserRegisterParam userRegisterParam){
        passportService.register(userRegisterParam);
        return JsonResult.ok();
    }
}
